home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / prog_c / cuj0696.zip / DWYER.ZIP / COUPON.TST / GETGCD.C < prev    next >
C/C++ Source or Header  |  1996-02-23  |  962b  |  50 lines

  1. /* ============ */
  2. /* getgcd.c    */
  3. /* ============ */
  4. #include <defcodes.h>
  5. #include <math.h>
  6. /* ==================================================================== */
  7. /* GetGCD - finds greatest common divisor by Euclid's algorithn        */
  8. /* ==================================================================== */
  9. LDBL
  10. GetGCD(LDBL  m, LDBL  n)
  11. {
  12.     LDBL  t;
  13.  
  14.     m = floorl(fabsl(m)), n = floorl(fabsl(n));
  15.  
  16.    if (m < n)
  17.    {
  18.     t = m; m = n; n = t;
  19.     }
  20.     while (n > 0)
  21.     {
  22.     t = fmodl(m, n);
  23.     m = n, n = t;
  24.     }
  25.  
  26.     return (m);
  27. }
  28. # undef TEST
  29. # ifdef TEST
  30. #define    MAIN    main
  31. #define    GETLDBL GetLDBL
  32. #include <miscdefs.h>
  33. void
  34. MAIN()
  35. {
  36.     LDBL u, v;
  37.  
  38.     printf("Testing Euclid's algorithm:  "
  39.        "Needs two integers per call\n\n");
  40.  
  41.     while (main)
  42.     {
  43.     GETLDBL("Enter first of the next pair: ", &u);
  44.     GETLDBL("Enter the second:             ", &v);
  45.  
  46.     printf("GCD = %.Lf\n", GetGCD(u,v));
  47.     }
  48. }
  49. # endif
  50.